import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logger/logger.dart';
import 'package:my_test_demo/util/LoadingUtils.dart';
import 'package:provider/provider.dart';

import '../../widget/textfield/TextFieldAccount.dart';
import '../../widget/textfield/TextFieldPwd.dart';
import '../constant/constant.dart';
import '../constant/router_constant.dart';
import '../http/dio_manager.dart';
import '../http/service_url.dart';
import '../provider/login_provider.dart';
import '../util/toast_util.dart';
import '../util/user_util.dart';

class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<LoginBtnProvider>(
      create: (_) => LoginBtnProvider(),
      child: const LoginScreen(),
    );
  }
}

class LoginScreen extends StatelessWidget {
  const LoginScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //登录时保存软键盘高度,在聊天界面第一次弹出底部布局时使用
    // print("键盘高度2是:${MediaQuery.of(context).viewInsets.bottom}");
    //  final viewInsets = EdgeInsets.fromWindowPadding(WidgetsBinding.instance.window.viewInsets,WidgetsBinding.instance.window.devicePixelRatio);
    // print("键盘高度是:$viewInsets");
    // SpUtil.putDouble( Constant.SP_KEYBOARD_HEGIHT, MediaQuery.of(context).viewInsets.bottom);
    return SafeArea(
      child: Material(
        child: Scaffold(
          backgroundColor: Colors.white,
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                const TitleWidget(),
                Container(
                  margin: const EdgeInsets.only(left: 20.0, top: 30.0, bottom: 20),
                  child: Text("请输入账号密码", style: TextStyle(fontSize: 24.0, color: Colors.black)),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10, left: 20, right: 20),
                  child: AccountEditText(
                    contentStrCallBack: (content) {
                      context.read<LoginBtnProvider>().setInputAccount(content);
                    },
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10, left: 20, right: 20),
                  child: PwdEditText(
                    contentStrCallBack: (content) {
                      context.read<LoginBtnProvider>().setInputPwd(content);
                    },
                  ),
                ),
                LoginBtn(),
                RegistForgetWidget(),
                OtherLoginWidget(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class TitleWidget extends StatelessWidget {
  const TitleWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween, //子组件的排列方式为主轴两端对齐
      children: <Widget>[
        InkWell(
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Image.asset('${Constant.ASSETS_IMG}icon_close.png', width: 20.0, height: 20.0),
          ),
          onTap: () {
            if (Navigator.canPop(context)) {
              Navigator.pop(context);
            } else {
              SystemNavigator.pop();
            }
          },
        ),
        InkWell(
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Text("帮助", style: TextStyle(fontSize: 16.0, color: Color(0xff6B91BB))),
          ),
          onTap: () {},
        ),
      ],
    );
  }
}

class RegistForgetWidget extends StatelessWidget {
  const RegistForgetWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween, //子组件的排列方式为主轴两端对齐
      children: <Widget>[
        InkWell(
          child: Padding(
            padding: const EdgeInsets.only(left: 20.0, top: 3),
            child: Text("注册", style: TextStyle(fontSize: 13.0, color: Color(0xff6B91BB))),
          ),
          onTap: () {},
        ),
        InkWell(
          child: Padding(
            padding: const EdgeInsets.only(right: 20.0, top: 3),
            child: Text("忘记密码", style: TextStyle(fontSize: 13.0, color: Color(0xff6B91BB))),
          ),
          onTap: () {},
        ),
      ],
    );
  }
}

class OtherLoginWidget extends StatelessWidget {
  const OtherLoginWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 150),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Container(
                  margin: EdgeInsets.only(left: 20),
                  color: Color(0xffEAEAEA),
                  height: 1,
                ),
              ),
              Expanded(
                flex: 1,
                child: Center(
                  child: Text('其他登陆方式', style: TextStyle(fontSize: 12, color: Color(0xff999999))),
                ),
              ),
              Expanded(
                flex: 1,
                child: Container(
                  margin: EdgeInsets.only(right: 20),
                  color: Color(0xffEAEAEA),
                  height: 1,
                ),
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            //crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  // Navigator.pop(context);
                  // Navigator.pushNamed(context, RouterConstant.INDEX);
                },
                child: Container(
                  margin: EdgeInsets.only(right: 20, top: 10),
                  child: Column(
                    children: <Widget>[
                      Image.asset(
                        '${Constant.ASSETS_IMG}login_weixin.png',
                        width: 40.0,
                        height: 40.0,
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 5),
                        child: Text('微信', style: TextStyle(fontSize: 12, color: Color(0xff999999))),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 20, top: 10),
                child: Column(
                  children: <Widget>[
                    Image.asset('${Constant.ASSETS_IMG}login_qq.png', width: 40.0, height: 40.0),
                    Container(
                      margin: EdgeInsets.only(top: 5),
                      child: Text('QQ', style: TextStyle(fontSize: 12, color: Color(0xff999999))),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class LoginBtn extends StatelessWidget {
  const LoginBtn({super.key});

  @override
  Widget build(BuildContext context) {
    var mCount = context.watch<LoginBtnProvider>().inputAccount;
    var mPwd = context.watch<LoginBtnProvider>().inputPwd;
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.only(left: 20.0, right: 20.0, top: 60.0),
      child: ElevatedButton(
        style: ButtonStyle(
          backgroundColor: WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
            if (states.contains(WidgetState.disabled)) {
              return Color(0xffFFD8AF);
            }
            return Color(0xffFF8200);
          }),
          foregroundColor: WidgetStateProperty.all(Colors.white),
          elevation: WidgetStateProperty.all(0),
        ),
        onPressed:
            (mCount.isEmpty || mPwd.isEmpty)
                ? null
                : () {
                  FormData params = FormData.fromMap({'username': mCount, 'password': mPwd});
                  DioManager.instance.post(
                    ServiceUrl.login,
                    params,
                    (data) {
                      UserUtil.saveUserInfo(data);
                      if (kDebugMode) {
                        print('登录成功!');
                      }
                      ToastUtil.show('登录成功!');
                      Navigator.pop(context);
                      Navigator.pushNamed(context, RouterConstant.INDEX);
                    },
                    (error) {
                      if (kDebugMode) {
                        print('登录失败!');
                        var logger = Logger();
                        logger.d("登录失败111");
                      }
                      ToastUtil.show(error);
                    },
                  );
                },
        child: Padding(
          padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
          child: Text("登录", style: TextStyle(fontSize: 16.0)),
        ),
      ),
    );
  }
}
